/*
 * user.cpp
 *
 *  Created on: 24-Dec-2010
 *      Author: sudhix
 */

#include <mainwindow.h>

UserClient::UserClient() : iMessageSession(0)/*, iMessageEventFilter(0)*/, iChatStateFilter(0), iConnected(false)
{
        er = ConnNoError;
}

UserClient::~UserClient()
{
        if(iMessageSession)
                j->disposeMessageSession(iMessageSession);
}

void UserClient::SetUserDetails(UserClientCredentials aUserCredentials)
{
        string tempjid(aUserCredentials.getUserName());
        tempjid.append("@");
        tempjid.append(aUserCredentials.getHost());
        JID temp(tempjid);
        iUserJidString = tempjid;
        iUserJid = temp;
        iUser = aUserCredentials;
}

void UserClient::SetDestinationUser(UserClientCredentials aUserCredentials)
{
        string tempjid(aUserCredentials.getUserName());
        tempjid.append("@");
        tempjid.append(aUserCredentials.getHost());
        JID temp(tempjid);
        iDestJidString = tempjid;
        iDestJid = temp;
        iDestUser = aUserCredentials;
}

bool UserClient::ConnecttosServer()
{
        //JID jid( "map@jo-hely.hu/gloox" );

        j = new Client( iUserJid, iUser.getPassword() );
        j->registerMessageHandler( this);
        j->registerConnectionListener( this );
        j->registerMessageSessionHandler(this);
        bool result = j->connect();
        if(result == true)
        {
                DisplayMessage("Initial connection successful");
        }
        else
        {
                DisplayMessage("Connection to Server failed");
                DisplayMessage("Please re-try after some time");
                return false;
        }
        return true;
        //if result is false, connection failed
        //maybe user is not registered
        //register new user
}

void UserClient::handleMessage( const Message& aMsg, MessageSession* aSession )
{
        if(iMessageSession == NULL)
        {
                iMessageSession = aSession;
        }

        string m = aMsg.body();
        DisplayMessage(m);
        cout << "received"<<m;
        parent->DisplayReceivedMessage(m);
}

void UserClient::onConnect()
{
        DisplayMessage("Successful Login.");
        DisplayMessage("To Exit application, type \"Bye\" or \"bye\".");
        iConnected = true;
}
/*
bool UserClient::CompleteConnection()
{
        while(iConnected == false)
        {
                er = j->recv();
        }
        return true;
 }
void *RecvMsg1(void* athis)
{
        UserClient *t = (UserClient*)athis;
        //while(t->er == ConnNoError)
        //while(t->iConnected == true)
        {
                t->er = t->j->recv(5000);
        //	sleep(3);
        }
}*/
void UserClient::onDisconnect(ConnectionError/* e */)
{
        DisplayMessage("Client Successfully Disconnected");
        DisplayMessage("Performing Cleanup");
        j->disposeMessageSession(iMessageSession);
        DisplayMessage("Goodbye.");
}

void UserClient::handleMessageSession( MessageSession* session )
{
        j->disposeMessageSession(iMessageSession);
        iMessageSession = session;
        iMessageSession->registerMessageHandler(this);
        iChatStateFilter = new ChatStateFilter(iMessageSession);
        iChatStateFilter->registerChatStateHandler(this);
}

//simply dummy for now
void UserClient::handleChatState( const JID& from, ChatStateType /*state */)
{
        DisplayMessage("Chat State Handler");
        DisplayMessage(from.full().c_str());
}

void UserClient::handleMessageEvent( const JID& from, MessageEventType /*event*/ )
{
        DisplayMessage("Message Event Handler");
        DisplayMessage(from.full().c_str());
}

void UserClient::handlePresence( const Presence& /*presence*/ )
{
        DisplayMessage("Presence Handler");

}

bool UserClient::onTLSConnect(const CertInfo& /*info*/)
{
        return true;
}

